from starlette.endpoints import HTTPEndpoint
from database import Session
import logging
from dao import DaoReadStatusCheck
from dao import DaoAreaList
from starlette.responses import JSONResponse
from wxpublic import wx_public_message
import config
from sqlalchemy.exc import DBAPIError
import calendar,datetime



class ReadStatusCheckSupervise(HTTPEndpoint):
    async def get(self, request):
        session = Session()
        try:
            openid = request.query_params["openid"]
            bureauid = request.query_params["bureauid"]
            flag = '1'#1督办2周报3月报
            read_flag = DaoReadStatusCheck.get_read_check(session, bureauid, openid, flag)
            print(f'Supervise:{read_flag}')
            return JSONResponse({"result": read_flag})
        finally:
            session.close()

class ReadStatusCheckWeekly(HTTPEndpoint):
    async def get(self, request):
        session = Session()
        try:
            openid = request.query_params["openid"]
            bureauid = request.query_params["bureauid"]
            flag = '2'  # 1督办2周报3月报
            read_flag = DaoReadStatusCheck.get_read_check(session, bureauid, openid, flag)
            print(f'Weekly:{read_flag}')
            return JSONResponse({"result": read_flag})
        finally:
            session.close()

class ReadStatusCheckMonth(HTTPEndpoint):
    async def get(self, request):
        session = Session()
        try:
            openid = request.query_params["openid"]
            bureauid = request.query_params["bureauid"]
            flag = '3'  # 1督办2周报3月报
            read_flag = DaoReadStatusCheck.get_read_check(session, bureauid, openid, flag)
            return JSONResponse({"result": read_flag})
        finally:
            session.close()

    async def put(self, request):
        session = Session()
        try:
            dict_rec = await request.json()
            bureauid = dict_rec.get("bureauid")
            flag = dict_rec.get("flag")
            openid = dict_rec.get("openid")
            DaoReadStatusCheck.update_read_flag(session, bureauid, openid, flag)
            session.commit()
        except Exception:
            session.rollback()
            return JSONResponse({"result": "0"})
        else:
            return JSONResponse({"result": "1"})
        finally:
            session.close()